package com.limeng.server;

import java.io.*;
import java.sql.ResultSet;

public class Response {

    private String code;

    private OutputStream outputStream;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void writeHtml(String url) throws IOException {
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(url);
        File file = new File(absoluteResourcePath);
        FileInputStream inputStream = new FileInputStream(file);
        int count = 0;
        while(count==0){
            count = inputStream.available();
        }

        int resourceSize = count;
        outputStream.write((ResponseUtil.return200(resourceSize)).getBytes());

        long written = 0;
        int byteSize = 1024;
        byte[] b = new byte[byteSize];
        while(written<resourceSize){
            if(written+byteSize>resourceSize){
                byteSize = (int) (resourceSize - written);
                b = new byte[byteSize];
            }
            inputStream.read(b);
            outputStream.write(b);
            outputStream.flush();
            written+=byteSize;
        }
        outputStream.close();
    }


    public void output(String content) throws IOException {
        outputStream.write(ResponseUtil.return200(content.length()).getBytes());
        outputStream.write(content.getBytes());
    }

}
